package com.demo.phonerepair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.phonerepair.mapper.UserMapper;
import com.demo.phonerepair.module.LoginUser;
import com.demo.phonerepair.module.RegisterUser;
import com.demo.phonerepair.pojo.User;
import com.demo.phonerepair.service.UserService;
import com.demo.phonerepair.util.Md5Password;
import com.demo.phonerepair.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginUser login(String usernameOrEmail, String password) {
        // 统一使用 user_email 字段名
        User user = this.getOne(new QueryWrapper<User>()
                .eq("user_name", usernameOrEmail)
                .or()
                .eq("user_email", usernameOrEmail));
        //返回错误信息
        if (user == null) {
            return null;
        }
        if (!user.getUserPasswordHash().equals(Md5Password.generateMD5(password))) {
            return null;
        }
        //返回正确信息
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(user.getUserId());
        loginUser.setUserName(user.getUserName());
        loginUser.setUserEmail(user.getUserEmail());
        loginUser.setRoleId(user.getRoleId());
        loginUser.setUserBio(user.getUserBio());
        loginUser.setUserPhone(user.getUserPhone());
        return loginUser;
    }

    @Override
    public Result<String> register(RegisterUser registerUser) {
        User user = this.getOne(new QueryWrapper<User>()
                .eq("user_name", registerUser.getUserName())
                .or()
                .eq("user_email", registerUser.getUserEmail()));
        if (user != null) {
            return Result.fail("用户已存在", 500);
        }
        // 修复正则表达式，移除多余的右括号 ]
        if (registerUser.getPasswordHash().length() < 6 || !registerUser.getPasswordHash().matches(".*[a-zA-Z]+.*")) {
            return Result.fail("密码长度必须大于 6，必须包含字母", 500);
        }
        user = new User();
        user.setUserName(registerUser.getUserName());
        user.setUserEmail(registerUser.getUserEmail());
        user.setUserPasswordHash(Md5Password.generateMD5(registerUser.getPasswordHash()));
        user.setRoleId("2");
        boolean save = this.save(user);
        if (save) {
            return Result.success("注册成功！");
        }
        return Result.fail("注册失败", 500);
    }


    @Override
    public Result<Map<String, Object>> getAllUser(int userId, String searchKeyword,
                                                  Integer pageNum, Integer pagesize, String sortField, String sortOrder) {
        //判断参数是否为空
        pageNum = pageNum == null || pageNum < 1 ? 1 : pageNum;
        pagesize = pagesize == null || pagesize < 1 ? 10 : pagesize;
        searchKeyword = searchKeyword == null ? "" : searchKeyword;
        if (sortField == null || sortField.trim().isEmpty()) {
            sortField = "createdAt";
        }
        if (sortOrder == null || (!"asc".equalsIgnoreCase(sortOrder) && !"desc".equalsIgnoreCase(sortOrder))) {
            sortOrder = "desc";
        }
        Page<User> page = new Page<>(pageNum, pagesize);
        Map<String, Object> map = new HashMap<>();
        IPage<User> pageResult = userMapper.selectAllUser(page, userId, searchKeyword, sortField, sortOrder);
        map.put("count", pageResult.getTotal());
        map.put("userList", pageResult.getRecords());
        return Result.success(map);
    }


    @Override
    public Result<String> updateUser(User user) {
        //判断参数是否为空
        if (user.getUserId() == null)
            return Result.fail("用户id不能为空", 500);
        if (user.getUserEmail() == null)
            return Result.fail("用户邮箱不能为空", 500);//判断Email是否是除自己以外的在使用
        User user1 = this.getOne(new QueryWrapper<User>().eq("user_id", user.getUserId()));
        if (user1 == null)
            return Result.fail("用户不存在", 500);//判断Email是否被其他人使用
        if (!user.getUserEmail().equals(user.getUserEmail())) {
            User user2 = this.getOne(new QueryWrapper<User>().eq("user_email", user.getUserEmail()));
            if (user2 != null) {
                return Result.fail("邮箱已存在", 500);
            }
        }
        int result = userMapper.updateUser(user);
        if (result > 0) {
            return Result.success("用户更新成功");
        } else {
            return Result.fail("用户更新失败，可能用户不存在", 404);

        }
    }

    @Override
    public int deleteUserByUserId(Integer userId) {
        //判断参数是否为空
        if (userId == null) {
            Result.fail("用户Id不能为空", 404);
        }
        //判断用户是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            Result.fail("用户记录不存在", 404);
        }
        int rows = userMapper.deleteUserByUserId(userId);
        if (rows <= 0) {
            Result.fail("删除失败", 404);
        }
        return rows;
    }
}